package com.clementheliou.cheliou.dal.model.member.enumeration;

/**
 * Roles that can be granted to an user.
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @since 1.0
 */
public enum MemberRole {

	/**
	 * The administrator role.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	ADMINISTRATOR("ROLE_ADMIN"),

	/**
	 * The anonymous role.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	ANONYMOUS("ROLE_ANONYMOUS"),

	/**
	 * The user role.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	USER("ROLE_USER");

	/**
	 * The role's code as it will be used in the application's security
	 * module(s).
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private String code;

	/**
	 * Constructor from the given code.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param code the code to be used.
	 * @since 1.0
	 */
	private MemberRole(final String code) {
		this.code = code;
	}

	/**
	 * Gets the enumeration matching the given code, if exists.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param code the code related to the enumeration we are looking for.
	 * @return the enumeration if exists, <code>null</code> otherwise.
	 * @since 1.0
	 */
	public static MemberRole valueOfCode(final String code) {
		for (MemberRole memberRole : MemberRole.values()) {
			if (memberRole.getCode().equals(code)) {
				return memberRole;
			}
		}
		return null;
	}

	/*
	 * Getters & setters.
	 */

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
